<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <div class="text-center">
              <userAvatar v-if="user.avatar" :initialAvatar="user.avatar"></userAvatar>
            </div>
            <el-descriptions :column="1" border>
              <el-descriptions-item label="用户名称">
                <el-icon :size="18"><User /></el-icon>
                <span>{{ user.userName }}</span>
              </el-descriptions-item>
              <el-descriptions-item label="手机号码">
                <el-icon :size="18"><Phone /></el-icon>
                <span>{{ user.phonenumber }}</span>
              </el-descriptions-item>
              <el-descriptions-item label="用户邮箱">
                <el-icon :size="18"><Message /></el-icon>
                <span>{{ user.email }}</span>
              </el-descriptions-item>
              <el-descriptions-item label="所属部门">
                <el-icon :size="18"><OfficeBuilding /></el-icon>
                <span v-if="user.dept">{{ user.dept.deptName }} / {{ postGroup }}</span>
              </el-descriptions-item>
              <el-descriptions-item label="所属角色">
                <el-icon :size="18"><UserFilled /></el-icon>
                <span>{{ roleGroup }}</span>
              </el-descriptions-item>
              <el-descriptions-item label="创建日期">
                <el-icon :size="18"><Calendar /></el-icon>
                <span>{{ user.createTime }}</span>
              </el-descriptions-item>
            </el-descriptions>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>修改信息</span>
          </div>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import userInfo from "@/components/user/userInfo/userInfo.vue";
import resetPwd from "@/components/user/userInfo/resetPwd.vue";
import { getUserProfile } from "@/apis/user";
import userAvatar from "@/components/user/userInfo/userAvatar.vue";
import {User, Calendar, OfficeBuilding, Message, Phone, UserFilled} from "@element-plus/icons-vue";

// 声明响应式引用
const user = ref({});
const roleGroup = ref({});
const postGroup = ref({});
const activeTab = ref("userinfo");

// 类似于 created 生命周期的函数
onMounted(async () => {
  try {
    const response = await getUserProfile();
    user.value = response.data;
    roleGroup.value = response.roleGroup;
    postGroup.value = response.postGroup;
  } catch (error) {
    console.error('获取用户信息失败:', error);
  }
});

// 假设你有一个改变 activeTab 的方法
function changeActiveTab(newTab) {
  activeTab.value = newTab;
}
</script>

<style scoped>
.app-container {
  height: 92vh;
  width: 195vh;
  padding: 4vh 0 0 4vh;
}

.el-descriptions__label {
  font-weight: bold;
}

.el-descriptions__content {
  display: flex;
  align-items: center;
}

.el-icon {
  margin-right: 8px;
}
</style>
